﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Microsoft.Phone.BackgroundTransfer;

namespace FondoDinamico.ViewModel
{
     public class DescargasPendientesViewModel:NotificationObject
    {


         private ObservableCollection<Solicitud> pendingRequest;

        public ObservableCollection<Solicitud> PendingRequest
        {
            get { return pendingRequest; }
            set
            {
                pendingRequest = value;
                OnPropertyChanged(this);
            }
        }

        public DescargasPendientesViewModel() {
            UpdateRequestsList();
        }


        private void UpdateRequestsList()
        {
            // The Requests property returns new references, so make sure that 
            // you dispose of the old references to avoid memory leaks.
            if (PendingRequest != null)
            {
                foreach (var request in PendingRequest)
                {
                    request.Request.Dispose();
                }
            }
            
            
            
            
           var aux = new ObservableCollection<Solicitud>();

            foreach (var item in BackgroundTransferService.Requests)
            {
                aux.Add(new Solicitud() { TransferStatus = item.TransferStatus.ToString(), BytesReceived = item.BytesReceived, Request = item });
            }

            PendingRequest = aux;

            AddHandlers();
        }

        private void AddHandlers()
        {
            foreach (var item in PendingRequest)
            {
                item.Request.TransferProgressChanged += item_TransferProgressChanged;
            }
        }

        public void removeHandlers()
        {
            foreach (var item in PendingRequest)
            {
                item.Request.TransferProgressChanged -= item_TransferProgressChanged;
            }
        }

        void item_TransferProgressChanged(object sender, BackgroundTransferEventArgs e)
        {

            foreach (var item in PendingRequest)
            {
                if (item.Request.RequestId.Equals(e.Request.RequestId))
                {
                    item.BytesReceived = e.Request.BytesReceived;
                    item.TransferStatus = e.Request.TransferStatus.ToString();
                    
                }
          }
           
        }

     
     }


     public class Solicitud:NotificationObject {

         private string status;

         public string TransferStatus    
         {
             get { return status; }
             set { status = value; 
             OnPropertyChanged(this);
             }
         }


         private long bytesReceived;

         public long BytesReceived 
         {
             get { return bytesReceived; }
             set { bytesReceived = value;
             OnPropertyChanged(this);
             }
         }
          
         private BackgroundTransferRequest request;

         public BackgroundTransferRequest Request
         {
             get { return request; }
             set { request = value; }
         }
         
     
     
     }

}
